package com.example.desinger.designPattern;

import com.example.desinger.entity.Students;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class genderGroupStratagem implements ISplitGroupStratagem {
    @Override
    public ArrayList splitGroup(int total, List<Students> studentsList) {
        // 优先按性别分组

        // 先排序
        studentsList.sort((o1, o2) -> o1.getSex() - o2.getSex());

        ArrayList<Map> groups = new ArrayList<>();
        // 每组多少人
        double groupSize = Math.round((double) studentsList.size() / (double) total);

        for (int i = 0; i < total; i++) {
            Map<String, Object> group = new HashMap();
            String groupName = "第" + (i + 1) + "组";
            int start = (int) (i * groupSize);
            int end = (int) ((i + 1) * groupSize) > studentsList.size() ? studentsList.size() : (int) ((i + 1) * groupSize);
            List members = studentsList.subList(start, end);
            group.put("groupName", groupName);
            group.put("members", members);
            groups.add(group);
        }

        return groups;
    }
}
